﻿using System;
using System.Xml.Linq;


public class Order
{
    public int OrderId { get; set; }
    public string OrderNumber { get; set; }
    public DateTime OrderDate { get; set; }
    public List<OrderDetails> OrderDetailsList { get; set; } = new List<OrderDetails>();
    public Customer Customer { get; set; }

    public Order(string orderNumber, Customer customer)
    {
        OrderNumber = orderNumber;
        OrderDate = DateTime.Now;
        Customer = customer;
        OrderDetailsList = new List<OrderDetails>();
    }

    public void AddOrderDetail(OrderDetails detail)
    {
        if (OrderDetailsList.Any(od => od.Equals(detail)))
        {
            throw new ArgumentException("订单明细重复。");
        }
        OrderDetailsList.Add(detail);
    }

    public bool RemoveOrderDetail(int productId)
    {
        var detailToRemove = OrderDetailsList.FirstOrDefault(od => od.ProductId == productId);
        if (detailToRemove != null)
        {
            OrderDetailsList.Remove(detailToRemove);
            return true;
        }
        return false;
    }

    public override string ToString()
    {
        return $"订单号: {OrderNumber}, 日期: {OrderDate.ToShortDateString()}, 客户: {Customer.Name}, 总金额: {TotalAmount:C}";
    }
    public bool Equals(Order other) { if (other == null) { return false; } return OrderNumber.Equals(other.OrderNumber); }
    public override bool Equals(object obj) { return Equals(obj as Order); }
    public override int GetHashCode() { return OrderNumber.GetHashCode(); }
    public decimal TotalAmount => OrderDetailsList.Sum(od => od.TotalAmount);
}

public class OrderDetails
{
    public int ProductId { get; set; }
    public Product Product { get; set; }
    public int Quantity { get; set; }
    public OrderDetails(int productId, Product product, int quantity) { ProductId = productId; Product = product; Quantity = quantity; }
    public decimal TotalAmount => Quantity * Product.Price; public override string ToString()
    {
        return $"{Product.Name} (数量: {Quantity}, 单价: {Product.Price:C}, 总金额: {TotalAmount:C})";
    }

    public bool Equals(OrderDetails other)
    {
        if (other == null)
        {
            return false;
        }
        return ProductId == other.ProductId && Quantity == other.Quantity;
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as OrderDetails);
    }

    public override int GetHashCode()
    {
        int hash = 17;
        hash = hash * 23 + ProductId.GetHashCode();
        hash = hash * 23 + Quantity.GetHashCode();
        return hash;
    }
}

public class OrderService
{
    public List<Order> _orders = new List<Order>();
    private List<Customer> _customers = new List<Customer>();
    private List<Product> _products = new List<Product>();

    public void AddOrder(Order order)
    {
        if (_orders.Any(o => o.OrderNumber == order.OrderNumber))
        {
            throw new ArgumentException("订单号已存在。");
        }
        _orders.Add(order);
    }

    public void DeleteOrder(string orderNumber)
    {
        var order = _orders.FirstOrDefault(o => o.OrderNumber == orderNumber);
        if (order == null)
        {
            throw new ArgumentException("订单不存在");
        }
        _orders.Remove(order);
    }

    public void ModifyOrder(string orderNumber, Action<Order> modificationAction)
    {
        var order = _orders.FirstOrDefault(o => o.OrderNumber == orderNumber);
        if (order == null)
        {
            throw new ArgumentException("订单不存在");
        }
        modificationAction(order);
    }

    public List<Order> QueryOrders(Func<Order, bool> queryFunc)
    {
        return _orders.Where(queryFunc).OrderByDescending(o => o.TotalAmount).ToList();
    }

    public List<Order> SortOrdersByOrderNumber()
    {
        return _orders.OrderBy(o => o.OrderNumber).ToList();
    }

    public Customer AddCustomer(string name, string contactInfo)
    {
        var newCustomer = new Customer(_customers.Count + 1, name, contactInfo);
        _customers.Add(newCustomer);
        return newCustomer;
    }

    public Product AddProduct(string name, decimal price)
    {
        var newProduct = new Product(_products.Count + 1, name, price);
        _products.Add(newProduct);
        return newProduct;
    }

    public void AddOrderDetail(Order order, Product product, int quantity)
    {
        var detail = new OrderDetails(product.ProductId, product, quantity);
        if (order.OrderDetailsList.Any(od => od.Equals(detail)))
        {
            throw new ArgumentException("订单明细重复。");
        }
        order.AddOrderDetail(detail);
    }

    public bool RemoveOrderDetail(Order order, int productId)
    {
        return order.RemoveOrderDetail(productId);
    }
}
public class Customer
{
    public int CustomerID { get; set; }
    public string Name { get; set; }
    public string ContactInfo { get; set; }

    public Customer(int customerId, string name, string contactInfo)
    {
        CustomerID = customerId;
        Name = name;
        ContactInfo = contactInfo;
    }

    public override string ToString()
    {
        return $"客户ID: {CustomerID}, 姓名: {Name}, 联系方式: {ContactInfo}";
    }
}
public class Product{   
    public int ProductId { get; set; }   
    public string Name { get; set; }    
    public decimal Price { get; set; }    
    public Product(int productId, string name, decimal price)
{        
        ProductId = productId;        
        Name = name;        
        Price = price;    
    }    
    public override string ToString()
    {        
        return $"商品ID: {ProductId}, 名称: {Name}, 价格: {Price:C}";
    }
}

static class Program
{
    static void Main(string[] args)
    {
        var orderService = new OrderService();
        Console.WriteLine("欢迎使用订单管理系统。请选择操作：");
        Console.WriteLine("1. 添加订单");
        Console.WriteLine("2. 删除订单");
        Console.WriteLine("3. 修改订单");
        Console.WriteLine("4. 查询订单");
        Console.WriteLine("5. 退出");

        int choice;
        while (true)
        {
            choice = int.Parse(Console.ReadLine());
            switch (choice)
            {
                case 1:
                    CreateOrder(orderService);
                    break;
                case 2:
                    DeleteOrder(orderService);
                    break;
                case 3:
                    Modifyorder(orderService);
                    break;
                case 4:
                    Queryorders(orderService);
                    break;
                case 5:
                    Console.WriteLine("感谢使用订单管理系统，再见！");
                    return;
                default:
                    Console.WriteLine("无效的输入，请重新选择。");
                    break;
            }
        }
    }

    private static void QueryOrders(OrderService orderService)
    {
        throw new NotImplementedException();
    }

    private static void ModifyOrder(OrderService orderService)
    {
        throw new NotImplementedException();
    }

    static void CreateOrder(OrderService orderService)
    {
        Console.WriteLine("请输入订单号：");
        string orderNumber = Console.ReadLine();
        Console.WriteLine("请输入客户姓名：");
        string customerName = Console.ReadLine();
        Console.WriteLine("请输入客户联系方式：");
        string contactInfo = Console.ReadLine();
        Customer customer = orderService.AddCustomer(customerName, contactInfo);

        Console.WriteLine("请输入商品名称：");
        string productName = Console.ReadLine();
        Console.WriteLine("请输入商品数量：");
        int quantity = int.Parse(Console.ReadLine());
        Product product = orderService.AddProduct(productName, 0); // 假设价格在添加商品时已设定
        product.Price = 10; // 假设商品价格为10元

        Order order = new Order(orderNumber, customer);
        orderService.AddOrderDetail(order, product, quantity);
        orderService.AddOrder(order);
        Console.WriteLine("订单创建成功！");
    }

    static void DeleteOrder(OrderService orderService)
    {
        Console.WriteLine("请输入要删除的订单号：");
        string orderNumber = Console.ReadLine();
        try
        {
            orderService.DeleteOrder(orderNumber);
            Console.WriteLine("订单删除成功！");
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    static void Modifyorder(OrderService orderService)
    {
        Console.WriteLine("请输入要修改的订单号：");
        string orderNumber = Console.ReadLine();
        try
        {
            Console.WriteLine("请输入新的客户姓名：");
            string customerName = Console.ReadLine();
            Console.WriteLine("请输入新的客户联系方式：");
            string contactInfo = Console.ReadLine();
            Customer newCustomer = orderService.AddCustomer(customerName, contactInfo);
            orderService.ModifyOrder(orderNumber, o => o.Customer = newCustomer);
            Console.WriteLine("订单修改成功！");
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    static void Queryorders(OrderService orderService)
    {
        Console.WriteLine("请输入查询条件（订单号/商品名称/客户姓名）：");
        string query = Console.ReadLine();
        try
        {
            var queryResult = orderService.QueryOrders(o => o.OrderNumber == query ||
                                                      o.OrderDetailsList.Any(od => od.Product.Name == query ||
                                                                  o.Customer.Name == query));
            foreach (var order in queryResult)
            {
                Console.WriteLine(order);
            }
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
